package com.beikai.demo01.collection;


import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: my-product
 * @author: xiaobei
 * @create: 2021-04-20 23:01
 * @description: 集合相关demo
 **/
public class CollectionDemo {
    public static void main(String[] args) throws InterruptedException {
        CollectionDemo collectionDemo = new CollectionDemo();
        // collectionDemo.hashMapDemo();
        // collectionDemo.synMap();
        // collectionDemo.displacement();
        // collectionDemo.threadDemo();
        // collectionDemo.threadDemo3();

       //  collectionDemo.checkThread();
        List<String> string = new ArrayList<>();
        string.add("张三");
        string.add("张三1");
        string.add("张三2");
        string.add("张三3");
        string.add("张三4");
        Iterator<String> iterator = string.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            if (next.contains("2")){
                string.remove("张三3");
            }
            System.out.println(iterator.next());
        }
    }

    private void checkThread() throws InterruptedException {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    java.lang.Thread.sleep(20000);
                    System.out.println("执行完成");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        java.lang.Thread thread = new java.lang.Thread(runnable);

        System.out.println("线程执行前 : "+thread.isAlive());

        thread.start();

        java.lang.Thread.sleep(15000);
        System.out.println("线程执行后 : " + thread.isAlive());

        thread.stop();

        System.out.println("线程停止后 : " + thread.isAlive());

    }

    private void threadDemo3(){
        int i = 32 - 3;
        System.out.println(-1 << i);
        System.out.println(0 << i);
        System.out.println(1 << i);
        System.out.println(2 << i);
        System.out.println(3 << i);
    }

    /**
     * @param
     * @return void
     * @author xiaobei
     * @date 2021/4/20 23:03
     * @description 测试hashmap
     */
    public void hashMapDemo() {
        HashMap<String, String> hashMap = new HashMap<String, String>(64, 0.75f);
        hashMap.put("zhangsan", "张三");
        String zhangsan = hashMap.get("zhangsan");
        hashMap.remove("zhangsan");
    }

    /**
     * 测试安全map
     */
    public void synMap() {
        // 采用适配器模式,对每个hashmap的方式适配,添加同步关键字
        Map<String, String> stringStringMap = Collections.synchronizedMap(new HashMap<String, String>());

        Hashtable<String, String> hashtable = new Hashtable<String, String>(2, 0.75f);
        hashtable.put("zhangsan", "李四");
        hashtable.put("zhangsan2", "李四2");
        String zhangsan = hashtable.get("zhangsan");

        ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put("张三", "zxhangsan");
        concurrentHashMap.get("张三");

        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("张三", "张三");
        linkedHashMap.get("张三");

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("张三");
        String s = arrayList.get(0);

        ArrayList<String> tempArray = new ArrayList<>(2);
        tempArray.add("李四");
        tempArray.add("王五");
        tempArray.add("赵柳");

        Vector<Integer> vector = new Vector<>();
        vector.add(1);

        tempArray.add(5, "赵子龙");

        String s1 = arrayList.get(1);

        HashSet<String> strings = new HashSet<>();
        strings.add("张三");
        Iterator<String> iterator = strings.iterator();

        for (String string : strings) {

        }



    }

    /**
     * @param
     * @return void
     * @author xiaobei
     * @date 2021/4/21 22:29
     * @description 位移测试
     */
    public void displacement() {

        int i = 20 >>> 2; // 5
        System.out.println(i);

        int i1 = -20 >>> 2; // 1073741819
        System.out.println(i1);
        /**
         * 　-20: 源码：10000000  00000000   00000000   00010100
         * 　　　　反码：11111111  11111111   11111111   11101011
         * 　　　　补码：11111111  11111111   11111111   11101100
         * 　　　　右移：00111111  11111111   11111111   11111011
         *        >>> 补码移动后的值
         * 　　　　结果：r = 1073741819
         */

        int i2 = -20 << 2; // -80
        System.out.println(i2);

        int i3 = 20 << 2; // 80
        System.out.println(i3);

        int i4 = -20 >> 2; // -5
        System.out.println(i4);

        int i5 = 20 >> 2;  // 5
        System.out.println(i5);

        System.out.println(~5);

        System.out.println(5 ^ 6);
        System.out.println(6 ^ 5);

        int i6 = 5;
        int i7 = 6;
        i6 = i6 ^ i7;
        i7 = i7 ^ i6;
        i6 = i6 ^ i7;

        System.out.println(i6);
        System.out.println(i7);
    }

    private void threadDemo() {
        // System.out.println(ThreadLocalRandom.current().nextInt());

        // new ThreadPoolExecutor()
        System.out.println("处理器核心数是 : " + Runtime.getRuntime().availableProcessors());
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8, 16, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NameFactory("我的线程池"));
        try {
            for (int i = 0; i < 100; i++) {
                int temp = i;
                threadPoolExecutor.submit(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
                threadPoolExecutor.execute(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        } finally {
            threadPoolExecutor.shutdown();
        }
        // new LinkedBlockingQueue<Runnable>()
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        ExecutorService executorService3 = Executors.newFixedThreadPool(8, Executors.defaultThreadFactory());

        // new SynchronousQueue<Runnable>()
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        ExecutorService executorService5 = Executors.newCachedThreadPool(Executors.defaultThreadFactory());
        // new DelayedWorkQueue()
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8);
        ScheduledExecutorService scheduledExecutorService3 = Executors.newScheduledThreadPool(8, Executors.defaultThreadFactory());
        ScheduledExecutorService scheduledExecutorService4 = Executors.newSingleThreadScheduledExecutor();
        ScheduledExecutorService scheduledExecutorService5 = Executors.newSingleThreadScheduledExecutor(Executors.defaultThreadFactory());


        // new LinkedBlockingQueue<Runnable>())
        ExecutorService executorService2 = Executors.newSingleThreadExecutor();
        ExecutorService executorService4 = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
        ScheduledExecutorService scheduledExecutorService2 = Executors.newSingleThreadScheduledExecutor();
        ScheduledExecutorService scheduledExecutorService6 = Executors.newSingleThreadScheduledExecutor(Executors.defaultThreadFactory());

        // new DelayedWorkQueue()
        ScheduledExecutorService scheduledExecutorService1 = Executors.newSingleThreadScheduledExecutor();

    }

    private void threadDemo2() {
        Runnable runnable = new Runnable() {
            int i = 1;

            @Override
            public void run() {
                try {
                    System.out.println(i);
                    i++;
                    // i为5时发生by zero异常
                    if (i == 5) {
                        int a = 1 / 0;
                    }
                } catch (Exception e) {
                    System.out.println("发生异常");
                }

            }

            ;
        };
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        // TimeUnit.SECONDS 延时单位为秒
        service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
    }

}

/**
 * @author xiaobei
 * @return
 * @date 2021/4/26 23:10
 * @description 自定义命名factory
 */
class NameFactory implements ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    NameFactory(String name) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();
        namePrefix = name + "-" +
                poolNumber.getAndIncrement() +
                "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,
                namePrefix + threadNumber.getAndIncrement(),
                0);
        if (t.isDaemon()) {
            t.setDaemon(false);
        }
        if (t.getPriority() != Thread.NORM_PRIORITY) {
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }
}
